#ifndef PARSER_H
#define PARSER_H


const vector<Data> MESSAGE_DEFINE{
    Data(DataType::Int8, "shutdown_time"),
    Data(DataType::Int32, "shutdown_time2"),
    Data(DataType::String, "text"),
    Data(DataType::Float32, "num"),
    Data(DataType::String, "text2"),
    Data(DataType::Int8Array, "data"),
    Data(DataType::Int16Array, "data2")
};

template<class T>
void print(const string &name, T t)
{
    cout << name << ": " << t << endl;
}

void printVector(const string &name, const vector<int> &data)
{
    cout << name << ": [";
    for (int index: data)
    {
        cout << index << ",";
    }
    cout << "]" << endl;
}

void parserInt8(const string &name, ByteBuf &origin_data)
{
    int8_t data = origin_data.readChar();
    print(name, (int) data);
}
void parserInt32(const string &name, ByteBuf &origin_data)
{
    int32_t data = origin_data.readIntLE();
    print(name, data);
}
void parserFloat32(const string &name, ByteBuf &origin_data)
{
    float data = origin_data.readFloatLE();
    print(name, data);
}

void parserString(const string &name, ByteBuf &origin_data)
{
    string data;
    int8_t ch;
    int32_t length = origin_data.readIntLE();
    for (int32_t index = 0; index < length; index++)
    {
        ch = origin_data.readChar();
        data.push_back(ch);
    }
    print(name, data);
}

void parserInt8Array(const string &name, ByteBuf &origin_data)
{
    vector<int> data;
    int32_t length = origin_data.readIntLE();
    for (int32_t index = 0; index < length; index++)
    {
        int8_t readChar = origin_data.readChar();
        data.push_back(readChar);
    }
    printVector(name, data);
}

void parserInt16Array(const string &name, ByteBuf &origin_data)
{
    vector<int> data;
    int32_t length = origin_data.readIntLE();
    for (int32_t index = 0; index < length; index++)
    {
        int16_t value = origin_data.readShortLE();
        data.push_back(value);
    }
    printVector(name, data);
}

bool parser(ByteBuf &origin_data)
{
    if (origin_data.readableBytes() < 4)
    {
        //数据长度不够
        return false;
    }
    int32_t length = origin_data.readIntLE();
    cout << "data length: " << length + 4 << endl;

    for (int index = 0; index < MESSAGE_DEFINE.size(); index++)
    {
        Data data = MESSAGE_DEFINE.at(index);
        string name = data.name;
        switch (data.type)
        {
            case DataType::Int8:
                parserInt8(name, origin_data);
                break;
            case DataType::Int32:
                parserInt32(name, origin_data);
                break;
            case DataType::Float32:
                parserFloat32(name, origin_data);
                break;
            case DataType::String:
                parserString(name, origin_data);
                break;
            case DataType::Int8Array:
                parserInt8Array(name, origin_data);
                break;
            case DataType::Int16Array:
                parserInt16Array(name, origin_data);
                break;
            default:
                break;
        }
    }
    return true;
}

#endif //PARSER_H
